package com.siemens.industrial.web.mvc.controller.admin;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.siemens.industrial.model.admin.Event;
import com.siemens.industrial.model.admin.User;
import com.siemens.industrial.utils.*;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.admin.EventService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/24.
 */
@Controller
public class EventController {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Resource
    EventService eventService;

    @Resource
    AuthService authService;

    @RequestMapping(value = "/admin/api/v1/event/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEventList() {
        List<Event> events = eventService.getEvents();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(events).build();
    }

    @RequestMapping(value = "/admin/api/v1/event/subscription", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addEventSubscription(@RequestBody Map<String, Object> data) {
        User user = (User) authService.currentUser();

        eventService.addUserEvent(data, user);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/event/isSubscription", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEvents() {
        User user = (User) authService.currentUser();
        List<Map<String, Object>> subscriptions = eventService.getEventIsSubsription(user);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(subscriptions).build();
    }

    @RequestMapping(value = "/admin/event/list", method = RequestMethod.GET)
    public String getEventListPage() {
        return "admin/event_list";
    }

    @RequestMapping(value = "/admin/event/subscription", method = RequestMethod.GET)
    public String getSubscriptionPage(ModelMap modelMap) {
        String email = ((User) authService.currentUser()).getEmail();
        modelMap.put("email",email);
        return "admin/createSubscription";
    }
}
